import path from 'path'
import { defineConfig, loadEnv } from 'vite'

import createVitePlugins from './vite/plugins/index'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const viteEnv = loadEnv( mode, process.cwd())
  const { VITE_PORT, VITE_PUBLIC_PATH } = viteEnv

  return {
    build: {
      chunkSizeWarningLimit: 1500,
      assetsDir: './backend-web-page',
      rollupOptions: {
        output: {
          manualChunks( id ) {
            if ( id.includes( 'node_modules' )) {
              return id.toString().split( 'node_modules/' )[1].split( '/' )[0].toString();
            }
          }
        //   entryFileNames: 'assets/js/[name].[hash].js',
        //   chunkFileNames: 'assets/js/[name].[hash].js',
        //   assetFileNames: 'assets/[ext]/[name].[hash].[ext]',
        }
      },
      sourcemap: false
    },
    resolve: {
      alias: {
        '@': path.resolve( __dirname, 'src' )
      }
    },
    optimizeDeps: {
      exclude: ['vue']
    },
    // 全局样式
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/assets/styles/index.scss" as *;'
        }
      }
    },
    server: {
      base: VITE_PUBLIC_PATH || '/',
      open: false,
      port: VITE_PORT,
      host: '0.0.0.0',
      proxy: {
        // '/local': {
        //   target: 'http://192.168.20.248:8080',
        //   changeOrigin: true,
        //   // secure: false,
        //   rewrite: ( path ) => path.replace( /^\/local/, '' )
        // }
        // 'local': 'http://192.168.20.248:8080/'
      }
    },
    plugins: createVitePlugins( mode, 'build' === command )
  }
})
